package com.etone.autotesting.conditions.base;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author chenzhichao
 * 
 */
public abstract class CommonCondition implements ICondition {
	/**
	 * 当前搜索索引
	 */
	protected double curIndex = 1;
	/**
	 * 总笛的长度
	 */
	protected double length = 0;
	/**
	 * 数组长度
	 */
	protected Integer listLength = 0;
	/**
	 * 数组值长度
	 */
	protected List<Object> conditionList;
	/**
 * 
 */
	protected String ConditionKey;

	/**
	 * @param conditionList
	 *            the conditionList to set
	 */
	public void setConditionList(List<Object> conditionList) {
		this.conditionList = conditionList;
		if (conditionList != null)
			listLength = conditionList.size();
	}

	/**
	 * @return the conditionList
	 */
	public List<Object> getConditionList() {
		return conditionList;
	}

	public void setCurIndex(Integer curIndex) {
		this.curIndex = curIndex;
	}

	public double getJoinLength() {
		if (conditionList != null && length == 0) {
			Integer len = conditionList.size();
			length = Math.pow(2, len) - 1;
		}
		return length;
	}

	public void setConditionKey(String key) {
		ConditionKey = key;
	}

	/**
	 * 条件组合的结果
	 * 
	 * @return
	 */
	protected List<Object> getNextJoinList() {
		String curComString = "";
		if (curIndex > length)
			return conditionList;
		List<Object> nextConditionList = new ArrayList<Object>();		
		curComString = getIndexString(0, 0);
		curIndex++;
		String[] charArray = curComString.split("\\,");//，号分割
		Integer stringLength = charArray.length;
		for (int i = 0; i < stringLength; i++) {
			Integer curChar = Integer.parseInt(String.valueOf(charArray[i]));
			nextConditionList.add(conditionList.get(curChar));
		}
		return nextConditionList;
	}

	/**
	 * 
	 * @param searchLength
	 * @param builder
	 *            累加的组合
	 * @param searchIndex
	 *            查询索引
	 * @return
	 */
	protected String getIndexString(double searchLength, Integer... searchIndex) {
		int searchIndexLength = searchIndex.length;
		int lastSearchIndexValue = searchIndex[searchIndexLength - 1];
		int powValue = 0;
		double mathPowValue = 0;
		powValue = listLength - lastSearchIndexValue - 1;
		mathPowValue = Math.pow(2, powValue);
		double totalValue = mathPowValue + searchLength;
		Integer nextIndexValue = lastSearchIndexValue + 1;
		if (curIndex > totalValue) {
			searchIndex[searchIndexLength - 1] = nextIndexValue;
			return getIndexString(totalValue, searchIndex);
		} else if (curIndex <= totalValue) {
			if (searchLength + 1 == curIndex){
				StringBuilder builder = new StringBuilder();
				for(Integer value : searchIndex)
					builder.append(value+",");
				builder.deleteCharAt(builder.length()-1);	
				return builder.toString();
			}						
			List<Integer> list = new ArrayList<Integer>();
			for (Integer ier : searchIndex)
				list.add(ier);
			list.add(nextIndexValue);
			return getIndexString(searchLength + 1, list.toArray(new Integer[0]));
		}
		return "";
	}

	/**
	 * 返回下一个单值
	 * 
	 * @return
	 */
	protected Object getNextValue() {
		if (conditionList == null || conditionList.size() <= 0)
			return "";
		return conditionList.get((int) curIndex++);
	}
}
